<template>
  <div>

    <div style="margin: 10px 0">
      <el-input
          style="width: 200px"
          placeholder="请输入资产编号"
          suffix-icon="el-icon-search"
          v-model="assetNumber"
      ></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="assetId" label="ID" width="80"></el-table-column>
      <el-table-column prop="assetNumber" label="资产编号" width="200">
      </el-table-column>
      <el-table-column prop="name" label="设备名称">
      </el-table-column>
      <el-table-column prop="type" label="设备类型"></el-table-column>
      <el-table-column label="设备图片" width="150" class-name="image-column">
        <template slot-scope="scope">
          <img :src="local+scope.row.picture" alt="Image"/>
        </template>
      </el-table-column>
      <el-table-column prop="assetState" label="状态" width="120">
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="handleEdit(scope.row)">
            申报维修 <i class="el-icon-refresh-left"/>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="25%">
      <el-form ref="form" :model="form" label-width="80px" size="small">
        <el-form-item label="资产编号" >
          <el-input
              v-model="form.assetNumber"
              autocomplete="off"
              :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="资产名称" >
          <el-input
              v-model="form.name"
              autocomplete="off"
              :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型" >
          <el-input
              v-model="form.type"
              autocomplete="off"
              :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="申请时间" required >
          <el-col :span="11">
            <el-date-picker
                type="date"
                placeholder="选择日期" value-format="yyyy-MM-dd"
                v-model="form.addtime"
                autocomplete="off"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="维修原因" required >
          <el-input
              type="textarea"
              v-model="form.maintenanceReason"
              autocomplete="off"
              :rows="5"
              placeholder="填写原因"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="handleCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>


<script>
import {LocalHost} from "@/utils/tools";

export default {
  data() {
    return {
      local:LocalHost,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      assetNumber: "",
      dialogVisible: false, // 添加 dialogVisible 属性并设置初始值为 false
      form: {},
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request
          .get("/maintain/inpage", {
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              assetNumber: this.assetNumber,
            },
          })
          .then((res) => {
            if (res.success) {
              this.tableData = res.data.records;
              this.total = res.data.total;
            }
          });
    },
    reset() {
      this.assetNumber = "";
      this.load();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    },
    handleEdit(row) {
      this.form.assetNumber = row.assetNumber;
      this.form.name = row.name;
      this.form.type = row.type;
      this.form.maintenanceReason='设备损坏，无法正常使用'
      this.dialogVisible = true;
    },
    handleCancel() {
      this.dialogVisible = false;
      this.resetForm();
    },
    resetForm() {
      this.$refs.form.resetFields();
      this.form = {
        asset_number: "",
        name: "",
        type: "",
        addtime: "",
        reason: "",
      };
    },
    onSubmit() {
      if (!this.form.addtime) {
        this.$message.error("请填写必填字段");
        return;
      }

      this.request.post("/maintain", this.form).then((res) => {
        if (res.data) {
          this.$message.success("申请成功");
          this.dialogVisible = false; // 修正对话框可见属性名
          this.load();
        } else {
          this.$message.error("申请失败");
        }
      });
    },
  },
};
</script>

<style scoped>
.image-column img {
  width: 120px; /* 设置图片宽度 */
  height: 120px; /* 设置图片高度 */
}
</style>